package com.examplemartin.jpademo.controller.admin;

import com.examplemartin.jpademo.bean.AjaxResult;
import com.examplemartin.jpademo.pojo.admin.OperatorLogPO;
import com.examplemartin.jpademo.pojo.po.MenuPO;
import com.examplemartin.jpademo.pojo.po.UserPO;
import com.examplemartin.jpademo.service.UserService;
import com.examplemartin.jpademo.service.admin.MenuService;
import com.examplemartin.jpademo.service.admin.OperatorLogService;
import com.examplemartin.jpademo.util.ValidateMember;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RequestMapping("/menu")
@Controller
@Slf4j
public class MenuController {

    @Resource
    private MenuService menuService;
    @Resource
    private UserService userService;
    @Resource
    private OperatorLogService operatorLogService;

    /**
     * 路由到菜单页 -- 包含菜单列表
     *
     * @param model
     *
     * @return
     */
    @GetMapping("/listmenu.do")
    public String getMenuList(Model model) {

//    MenuPO parent = menuService.getMenuById(1L);
//    List<MenuPO> menuParentList = menuService.getMenuByParent(parent);
        List<MenuPO> menuList = menuService.getMenuListOrder();
        model.addAttribute("menuList", menuList);
        model.addAttribute("title", "菜单管理");


        return "menu/listmenu";
    }

    /**
     * 路由到添加菜单页面
     *
     * @param model
     *
     * @return
     */
    @GetMapping("/addmenu.do")
    public String addMenu(Model model) {
        //获取所有顶级菜单
        List<MenuPO> menuByParent = menuService.getMenuByParent();
        model.addAttribute("menuByParent", menuByParent);
        model.addAttribute("title", "添加菜单");
        return "menu/addmenu";
    }

    /**
     * 添加新菜单 ajax
     *
     * @param menuPO
     * @param request
     *
     * @return
     */
    @PostMapping("/addmenu.ajax")
    @ResponseBody
    public AjaxResult addMenu(@RequestBody MenuPO menuPO, HttpServletRequest request) {


        if (ValidateMember.isEmpty(menuPO.getParent().getId()) || menuPO.getParent().getId() <= 0) {
            return AjaxResult.fail(500, "请选择正确的上级菜单");
        }

        if (ValidateMember.isEmpty(menuPO.getMenuname())) {
            return AjaxResult.fail(500, "请填写菜单名称");
        }

        if (ValidateMember.isEmpty(menuPO.getIcon())) {
            return AjaxResult.fail(500, "请选择菜单图标");
        }

        if (ValidateMember.isEmpty(menuPO.getUrl())) {
            return AjaxResult.fail(500, "请填写菜单地址URL");
        }

        MenuPO menuPO1 = menuService.insertMenuPO(menuPO);

        /**
         * 操作日志
         */
        // 判断 session和loginuserr
        HttpSession session = request.getSession();
        if (ValidateMember.isEmpty(session)) {
            return AjaxResult.fail(500, "有非法操作,请联系管理员");
        }


        UserPO loginuser = (UserPO) session.getAttribute("loginuser");

        if (ValidateMember.isEmpty(loginuser)) {
            return AjaxResult.fail(500, "有非法操作,请联系管理员");
        }

        OperatorLogPO logPO = new OperatorLogPO();
        logPO.setCreateTime(new Date());
        logPO.setContent("添加菜单:" + menuPO.getMenuname());


        UserPO userPO = userService.login(loginuser.getLoginname(), loginuser.getPassword());

        logPO.setUser(userPO);
        if (menuPO1 != null) {


            operatorLogService.saveLog(logPO);

            return AjaxResult.success(200, "添加新菜单成功", null);
        }
        logPO.setContent("添加菜单:" + menuPO.getMenuname() + "!!失败!!");
        operatorLogService.saveLog(logPO);
        return AjaxResult.fail(500, "没能正确添加新菜单,请联系管理员");
    }

    @GetMapping("/editmenu.do")
    public String editmenu(Long id, Model model) {
        MenuPO menuById = menuService.getMenuById(id);
        List<MenuPO> menuByParent = menuService.getMenuByParent();
        model.addAttribute("menuByParent", menuByParent);
        model.addAttribute("menu", menuById);
        model.addAttribute("title", "修改菜单-" + menuById.getMenuname());
        return "menu/editmenu";
    }

    @PostMapping("/savemenu.ajax")
    @ResponseBody
    public AjaxResult saveMenu(@RequestBody MenuPO menuPO, HttpServletRequest request) {


        if (ValidateMember.isEmpty(menuPO.getParent().getId()) || menuPO.getParent().getId() <= 0) {
            return AjaxResult.fail(500, "请选择正确的上级菜单");
        }

        if (ValidateMember.isEmpty(menuPO.getMenuname())) {
            return AjaxResult.fail(500, "请填写菜单名称");
        }

        if (ValidateMember.isEmpty(menuPO.getIcon())) {
            return AjaxResult.fail(500, "请选择菜单图标");
        }

        if (ValidateMember.isEmpty(menuPO.getUrl())) {
            return AjaxResult.fail(500, "请填写菜单地址URL");
        }
        MenuPO oldmen = menuService.getMenuById(menuPO.getId());
        menuPO.setCreateTime(oldmen.getCreateTime());
        menuPO.setId(oldmen.getId());
        MenuPO menuPO1 = menuService.saveMenuPO(menuPO);
        if (menuPO1 != null) {

            /**
             * 操作日志
             */
            // 判断 session和loginuserr
            HttpSession session = request.getSession();
            if (ValidateMember.isEmpty(session)) {
                return AjaxResult.fail(500, "有非法操作,请联系管理员");
            }


            UserPO loginuser = (UserPO) session.getAttribute("loginuser");

            if (ValidateMember.isEmpty(loginuser)) {
                return AjaxResult.fail(500, "有非法操作,请联系管理员");
            }

            OperatorLogPO logPO = new OperatorLogPO();
            logPO.setCreateTime(new Date());
            logPO.setContent("修改菜单:" + menuPO.getMenuname());


            UserPO userPO = userService.login(loginuser.getLoginname(), loginuser.getPassword());

            logPO.setUser(userPO);

            operatorLogService.saveLog(logPO);

            return AjaxResult.success(200, "修改菜单成功", null);
        }

        return AjaxResult.fail(500, "没能正确修改菜单,请联系管理员");
    }

    @GetMapping("/deletemenu.do")
    public String deletemenu(Long id, Model model) {
        log.warn("delete id = " + id);
        menuService.removeMenu(id);
        List<MenuPO> menuList = menuService.getMenuListOrder();
        model.addAttribute("menuList", menuList);
        model.addAttribute("title", "菜单管理");
        return "menu/listmenu";
    }

    @PostMapping("/deletemenu.ajax")
    @ResponseBody
    public AjaxResult deletemenuajax(@RequestBody MenuPO menuPO, HttpServletRequest request) throws JSONException {

        log.info("deletemenu.ajax id=" + menuPO.getId());
        Long id = menuPO.getId();
        if (ValidateMember.isEmpty(id)) {
            return AjaxResult.fail(500, "请重试，菜单id不存在");
        }

        MenuPO menuById = menuService.getMenuById(id);
        if (menuById == null) {
            return AjaxResult.fail(500, "请重试，菜单条目不存在");
        }

        List<MenuPO> menuByParent = menuService.getMenuByParent(menuById);
        if (menuByParent.size() > 0) {
            return AjaxResult.fail(500, "该菜单含有下级菜单，请先删除下级菜单");
        }

        try {
            menuService.removeMenu(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail(500, "有关联存在,删除失败");
        }


        //操作日志
        // 判断 session和loginuserr
        HttpSession session = request.getSession();
        if (ValidateMember.isEmpty(session)) {
            return AjaxResult.fail(500, "有非法操作,请联系管理员");
        }
        UserPO loginuser = (UserPO) session.getAttribute("loginuser");
        if (ValidateMember.isEmpty(loginuser)) {
            return AjaxResult.fail(500, "有非法操作,请联系管理员");
        }

        OperatorLogPO logPO = new OperatorLogPO();
        logPO.setCreateTime(new Date());
        logPO.setContent("成功删除菜单:" + menuById.getMenuname());

        log.warn("loginuser" + loginuser.toString());

        UserPO userPO = userService.login(loginuser.getLoginname(), loginuser.getPassword());

        logPO.setUser(userPO);

        operatorLogService.saveLog(logPO);

        return AjaxResult.success(200, "成功删除菜单", null);

    }


}
